package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;


    @Autowired
    private VodUploadClient vodUploadClient;
    /**
     * 上传声音
     * @param file
     * @return
     */
    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        Map<String,String> resultMap= null;

        try {
            //创建map封装数据
            resultMap = new HashMap<String,String>();
            //接收数据，存储本地，构建存储临时路径
            String tempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);

            //构建上传请求对象
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(tempPath);


            //上传
            VodUploadResponse response = vodUploadClient.upload(vodConstantProperties.getRegion(), request);
            //判断
            if(response!=null){

                //获取文件的唯一标识
                String mediaFileId = response.getFileId();
                //获取文件的播放地址
                String mediaUrl = response.getMediaUrl();
                resultMap.put("mediaFileId",mediaFileId);
                resultMap.put("mediaUrl",mediaUrl);

            }

            log.info("声音成功上传云点播,文件mediaFileId："+resultMap.get("mediaFileId"));
        } catch (Exception e) {
            log.error("声音上传失败,文件名称："+file.getOriginalFilename());
            throw new RuntimeException(e);
        }


        return resultMap;
    }

    /**
     * 根据声音ID获取媒体信息
     * @param mediaFileId
     * @return
     */
    @Override
    public TrackMediaInfoVo getMediaInfoByMediaFielId(String mediaFileId) {

        //创建封装对象
        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();

        try {

            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            Credential cred = new Credential(vodConstantProperties.getSecretId(),vodConstantProperties.getSecretKey());
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            //设置文件的ID
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);
            // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            //判断
            if(resp!=null){
                MediaInfo mediaInfo = resp.getMediaInfoSet()[0];
                //获取大小
                Long size = mediaInfo.getMetaData().getSize();
                //获取时长
                Float duration = mediaInfo.getMetaData().getDuration();
                //获取类型
                String type = mediaInfo.getBasicInfo().getType();
                trackMediaInfoVo.setDuration(duration);
                trackMediaInfoVo.setSize(size);
                trackMediaInfoVo.setType(type);
            }


            log.info("根据声音ID获取声音信息成功，声音ID："+mediaFileId);

        } catch (TencentCloudSDKException e) {
            log.error("根据声音ID获取声音信息失败，声音ID："+mediaFileId);
            throw new RuntimeException(e);
        }


        return trackMediaInfoVo;
    }

    /**
     * 删除云点播存储声音
     * @param mediaFileId
     */
    @Override
    public void deleteMediaInfoByMediaFielId(String mediaFileId) {


        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密

            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
                                                   //ap-guangzhou
            VodClient client = new VodClient(cred, "ap-guangzhou");
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);
            // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            DeleteMediaResponse resp = client.DeleteMedia(req);
            //判断
            if(resp!=null){
                String requestId = resp.getRequestId();
                log.info("删除云点播指定的声音成功，声音的id:{},请求处理id:{}",mediaFileId,requestId);

            }

        } catch (TencentCloudSDKException e) {
            log.error("删除云点播指定的声音失败，声音的id:{}",mediaFileId);
        }

    }
}
